package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ShouldPay;


public class ShouldPayManager extends HibernateDaoSupport {

	public void addShouldPay(ShouldPay shouldPay) {
		
		getHibernateTemplate().save(shouldPay);
	}
	
	public void updateShouldPay(ShouldPay shouldPay) {
		
		getHibernateTemplate().update(shouldPay);
	}
	
	public void deleteShouldPay(int id){
		
		getHibernateTemplate().delete(getShouldPay(id));
	}
	
	public ShouldPay getShouldPay(int id){
		ShouldPay d =(ShouldPay)getHibernateTemplate().get(ShouldPay.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<ShouldPay> listShouldPay() {
		return getHibernateTemplate().find("from ShouldPay");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		if(start==0 && limit==0){
			List <ShouldPay>list = getHibernateTemplate().find("from ShouldPay s where s.type = '" + type + "'" );
			int total = list.size();
			int toIndex = start + limit;
			if(toIndex>total){
				toIndex =  total;
			}
			List <ShouldPay>datas = list;
			PagerModel pm = new PagerModel();
			pm.setTotal(total);
			pm.setDatas(datas);
			return pm;
		}else{
			List <ShouldPay>list = getHibernateTemplate().find("from ShouldPay s where s.type = '" + type + "'" );
			ShouldPay sp = new ShouldPay();
			sp.setName("总计");
			sp.setId(1999999999);
			for (ShouldPay shouldPay : list) {
				sp.setDbalance(sp.getDbalance() + shouldPay.getDbalance());
				sp.setRbalance(sp.getRbalance() + shouldPay.getRbalance());
			}
			ShouldPay s = new ShouldPay();
			s.setName("本月合计");
			sp.setId(1999999998);
			SimpleDateFormat yy = new SimpleDateFormat("yyyy");
			SimpleDateFormat mm = new SimpleDateFormat("MM");
			String y = yy.format(new Date());
			String m = mm.format(new Date());
			List<ShouldPay> l = getHibernateTemplate().find("from  ShouldPay s where year(s.date) = '" + y + "' and month(s.date) = '" + m + "'and s.type = '" + type + "'" );
			for (ShouldPay shouldPay : l) {
				s.setDbalance(s.getDbalance() + shouldPay.getDbalance());
				s.setRbalance(s.getRbalance() + shouldPay.getRbalance());
			}
			s.setDbalance(fanyichu(s.getDbalance()));
			s.setRbalance(fanyichu(s.getRbalance()));
			sp.setDbalance(fanyichu(sp.getDbalance()));
			sp.setRbalance(fanyichu(sp.getRbalance()));
			list.add(s);
			list.add(sp);
			int total = list.size();
			int toIndex = start + limit;
			if(toIndex>total){
				toIndex =  total;
			}
			List <ShouldPay>datas = list.subList(start, toIndex);
			PagerModel pm = new PagerModel();
			pm.setTotal(total);
			pm.setDatas(datas);
			return pm;
		}
		

		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,ShouldPay shouldPay) {
		
		List <ShouldPay>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from ShouldPay r where r.type = '" + shouldPay.getType() + "'";
		if(!shouldPay.getName().equals("")){
			sql += "and r.name like '%" + shouldPay.getName() + "%'";
		}
		if(!shouldPay.getClerk().equals("")){
			
			sql += "and r.clerk = '" + shouldPay.getClerk() + "'";
		}
		if(shouldPay.getDate() != null && shouldPay.getEndDate() == null ){
			String d = sdf.format(shouldPay.getDate());
			
			sql += "and  r.date >= '" + d + "'";
		}
		if(shouldPay.getEndDate() != null && shouldPay.getDate() == null ){
			String d = sdf.format(shouldPay.getEndDate());
		
			sql += "and  r.date <= '" + d + "'";
		}
		if(shouldPay.getEndDate() != null && shouldPay.getDate() != null ){
			String d1 = sdf.format(shouldPay.getDate());
			String d2 = sdf.format(shouldPay.getEndDate());
			
			sql += "and r.date between '" + d1 + "' and '" +d2  +"'";
		}
		
		list = getHibernateTemplate().find(sql);
		ShouldPay sp = new ShouldPay();
		sp.setName("总计");
		sp.setId(1999999999);
		for (ShouldPay s : list) {
			sp.setDbalance(sp.getDbalance() + s.getDbalance());
			sp.setRbalance(sp.getRbalance() + s.getRbalance());
		}
		sp.setDbalance(fanyichu(sp.getDbalance()));
		sp.setRbalance(fanyichu(sp.getRbalance()));
		list.add(sp);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShouldPay>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	
	public double fanyichu(double d ){
		long l =(long)(d * 100);
		
		double dd = ((double)l)/100;
		return dd;
		
	}
	


}



